﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CCWin;

namespace DC.Views
{
    public partial class Form_OrderInfo : Skin_DevExpress
    {
        public Form_OrderInfo()
        {
            InitializeComponent();
        }

        Utils.Database DB = Utils.Global.GlobalDB;

        public string type = string.Empty;

        public string date = string.Empty;
        public string Time = string.Empty;
        public string DoctorID = string.Empty;

        public string OrderID = string.Empty;

        private void Form_OrderInfo_Load(object sender, EventArgs e)
        {
            if (type == "new")
            {
                this.Text = "新增预约";
                LoadInfo();
            }
            else
            {
                this.Text = "修改预约";
                DataView dv = DB.GetDs("select * from Book where Order_ID=" + OrderID).Tables[0].DefaultView;
                patientID = dv[0]["Patient_ID"].ToString();
                label6.Text = DB.GetData("select Patient_Name from Patient where Patient_ID=" + patientID).ToString();
                time_label.Text = Time;
                LoadInfo();
            }
        }

        private void LoadInfo()
        {
            if (type == "new")
            {
                this.Text = "新增预约";
            }
            else
            {
                this.Text = "修改预约";
                DataView dv = DB.GetDs("select * from Book where Order_ID=" + OrderID).Tables[0].DefaultView;
                patientID = dv[0]["Patient_ID"].ToString();
                label6.Text = DB.GetData("select Patient_Name from Patient where Patient_ID=" + patientID).ToString();
                textBox1.Text = dv[0]["Order_Things"].ToString();
                time_label.Text = Time;
            }
            time_label.Text = Time;
            label4.Text = DB.GetData("select Real_Name from [User] where User_ID=" + DoctorID).ToString();
        }


        private string patientID = string.Empty;
        private void ucBtnExt1_BtnClick(object sender, EventArgs e)
        {
            if (patientID == string.Empty)
            {
                MessageBox.Show("请先选择病人！", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (type == "new")
            {
                string sql = string.Format("insert into Book(Patient_ID,User_ID,Order_Time,Order_Things)values({0},{1},'{2}','{3}')",
                                           patientID,
                                           DoctorID,
                                           date + " " + time_label.Text,
                                           textBox1.Text);
                DB.Execute(sql);
                Close();
            }
            else
            {
                string sql = "update Book set Patient_ID=" + patientID + ",User_ID=" + DoctorID + ",Order_Things='" + textBox1.Text 
                    + "' where Order_ID=" + OrderID;
                DB.Execute(sql);
                Close();
            }
        }

        private void label6_Click(object sender, EventArgs e)
        {
            Form_SelectPatient form_SelectPatient = new Form_SelectPatient();
            form_SelectPatient.ShowDialog();
            if (form_SelectPatient.patientID == "0")
            {
                return;
            }
            else
            {
                patientID = form_SelectPatient.patientID;
                label6.Text = DB.GetData("select Patient_Name from Patient where Patient_ID=" + patientID).ToString();
            }
        }
    }
}
